package com.lyw.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyw.common.constant.WareConstant;
import com.lyw.gulimall.ware.entity.WareInfoEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyw.common.utils.PageUtils;
import com.lyw.common.utils.Query;

import com.lyw.gulimall.ware.dao.PurchaseDetailDao;
import com.lyw.gulimall.ware.entity.PurchaseDetailEntity;
import com.lyw.gulimall.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                new QueryWrapper<PurchaseDetailEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public IPage<Map<String, Object>> queryPurchasedetailPage(Integer page, Integer limit, String status, String wareId, String key) {
        Page<PurchaseDetailEntity> result = new Page<>(page, limit);
        LambdaQueryWrapper<PurchaseDetailEntity> lqw = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(status)) {
            lqw.eq(PurchaseDetailEntity::getStatus, status);
        }
        if (StringUtils.isNotBlank(wareId)) {
            lqw.eq(PurchaseDetailEntity::getWareId, wareId);
        }
        if (StringUtils.isNotBlank(key)) {
            lqw.and(wrapper -> {
                wrapper.eq(PurchaseDetailEntity::getId, key)
                        .or()
                        .like(PurchaseDetailEntity::getPurchaseId, key);
            });
        }
        IPage<Map<String, Object>> pages = baseMapper.selectPurchaseDetailPage(result, lqw);
        return pages;
    }

    @Override
    public void changeDetailListStatusByPurchaseId(Long id) {

        this.update(new LambdaUpdateWrapper<PurchaseDetailEntity>()
                .eq(PurchaseDetailEntity::getPurchaseId, id)
                .set(PurchaseDetailEntity::getStatus, WareConstant.PurchaseDetailsStatusEnum.BUYING.getCode()));
    }
}